﻿
Imports System.Collections.Specialized
Imports std

Namespace nvchelper
    Public Module nvchelper
        Private Sub copyToMapCB(ByVal name As String, ByVal value As String, ByVal m As map(Of String, list(Of String)))
            assert(Not m Is Nothing)
            m(name).push_back(value)
        End Sub

        Private Sub copyToMapCB(ByVal name As String, ByVal value As String, ByVal p As Object)
            copyToMapCB(name, value, convertor.cast(Of map(Of String, list(Of String)))(p))
        End Sub

        Public Function tomap(ByVal i As NameValueCollection) As map(Of String, list(Of String))
            Dim o As map(Of String, list(Of String)) = Nothing
            If copyToMap(i, o) Then
                Return o
            Else
                Return Nothing
            End If
        End Function

        Public Function copyToMap(ByVal i As NameValueCollection, ByRef o As map(Of String, list(Of String))) As Boolean
            If i Is Nothing Then
                Return False
            End If
            If o Is Nothing Then
                o = New map(Of String, list(Of String))()
            Else
                o.clear()
            End If
            traversal(i, AddressOf copyToMapCB, o)
            Return True
        End Function

        Public Function tonvc(ByVal i As map(Of String, list(Of String))) As NameValueCollection
            Dim o As NameValueCollection = Nothing
            If copyToNVC(i, o) Then
                Return o
            Else
                Return Nothing
            End If
        End Function

        Private Sub copyToNVCCB(ByVal name As String, ByVal value As String, ByVal i As NameValueCollection)
            assert(Not i Is Nothing)
            i.Add(name, value)
        End Sub

        Private Sub copyToNVCCB(ByVal name As String, ByVal value As String, ByVal p As Object)
            copyToNVCCB(name, value, convertor.cast(Of NameValueCollection)(p))
        End Sub

        Public Function copyToNVC(ByVal i As map(Of String, list(Of String)), ByRef o As NameValueCollection) As Boolean
            Return copyToNVC(i, o, AddressOf copyToNVCCB)
        End Function

        Public Function copyToNVC(ByVal i As map(Of String, list(Of String)),
                                  ByRef o As NameValueCollection,
                                  ByVal cb As traversalAction) As Boolean
            assert(Not cb Is Nothing)
            If i Is Nothing Then
                Return False
            End If
            If o Is Nothing Then
                o = New NameValueCollection()
            Else
                o.Clear()
            End If
            Dim it As map(Of String, list(Of String)).iterator = Nothing
            it = i.begin()
            While it <> i.end()
                Dim j As list(Of String).iterator = Nothing
                j = (+it).second.begin()
                While j <> (+it).second.end()
                    cb((+it).first(), (+j), o)
                    j += 1
                End While
                it += 1
            End While

            Return True
        End Function
    End Module
End Namespace
